package com.example.jwttest.controller;

import com.example.jwttest.config.SecurityConstants;
import com.example.jwttest.entity.User;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class UserController {
    @GetMapping("/login")
    public String login() {
        User user = new User();
        user.setId(1);
        user.setUsername("test");
        user.setPassword("pass");
        user.setUserText("这是个人说明");

        return Jwts.builder()
                .setSubject("Joe")
                .setExpiration(new Date(System.currentTimeMillis() + 864000000))
                .setIssuedAt(new Date())
                .claim("userId", user.getId())
                .signWith(Keys.hmacShaKeyFor(Decoders.BASE64.decode(SecurityConstants.JWT_SECRET)), SignatureAlgorithm.HS512)
                .compact();
    }
}
